Central processing unit architecture with enhanced branch prediction

ABSTRACT

A central processing unit (CPU) architecture with enhanced branch prediction, being substantially a pipelined CPU with multiple pipelines, each pipeline having a plurality of stages, by which all instructions relating directly to a branch instruction of a code executed by the pipelined CPU are being fetched respectively by each corresponding pipeline for enabling the code to be executed without stall so that the number of cycles required to execute the code can be reduced effectively. Moreover, the multiple pipelines can save more cycles when the number of stages in one pipeline is large.

FIELD OF THE INVENTION

The present invention relates to a central processing unit (CPU) architecture with enhanced branch prediction, being substantially a pipelined CPU with multiple pipelines, each pipeline having a plurality of stages, by which all instructions relating directly to a branch instruction of a code executed by the pipelined CPU are being fetched respectively by each corresponding pipeline for enabling the code to be executed without stall so that the number of cycles required to execute the code can be reduced effectively. Moreover, the multiple pipelines can save more cycles when the number of stages in one pipeline is large.

BACKGROUND OF THE INVENTION

Pipelining is a computer architecture implementation technique in which the execution of adjacent machine instructions is overlapped in time. Pipelining has been and continues to be the main technique for creating fast central processing units (CPUs), since it requires no change to compilers, linkers or programming techniques and yield is a tremendous performance improvement.

A pipelined CPU divides the execution of machine instructions into small, discrete steps or stages, whereas each stage can use only one cycle to execute. Each stage performs a fixed task or set of tasks on the machine instructions. Each stage expects that the tasks done by the preceding stage have been correctly and completely performed. The time required to do the tasks at each stage is less than the time it would take to completely execute the machine instruction. Therefore, it is noted that more stages will get better timing and get higher speed CPU.

In order to make pipelining work efficiently, it is necessary to keep all the stages full. However, there is a branch prediction problem whenever an instruction is encountered that alters the sequential flow of control in the program. If statements, loop statements, and procedure statements, which are BRANCH or JUMP instructions that requires one or more results being computed by the preceding instruction(s), cause problems with the pipeline. Consider the following code:

IF (condition A)

-   -   Execute procedure B

ELSE

-   -   Execute procedure C         It is note that a pipelined CPU must execute procedure B or C         based on the result of condition A. But the pipelined CPU can't         wait the result of condition A and then get the procedure B or C         instructions into pipelined stage. Thus, the pipelined CPU must         “prediction” the result of condition A and read one of the         procedure B or C instructions into pipeline, otherwise many         stages in the pipeline will be idle as the CPU waits the result         of condition A. If the CPU predicts condition A is true and         reads procedure B instructions into pipeline while condition A         is really true, then the CPU “hits” the result and finishes the         above IF-ELSE procedure quickly. But if the CPU predicts         condition A is true and reads procedure B instructions into         pipeline while condition A is false, then the CPU “misses” the         result and it must stop the execution of procedure B in the         pipeline and get the procedure C instructions into pipeline to         be executed, which will waste a lot of time. By virtue of this,         although pipelining is the technique for creating fast CPU, it         does not get equivalent performance.

Therefore, there is a need for an improvement for branch prediction in pipelined CPU by multiple pipelines.

SUMMARY OF THE INVENTION

It is the primary object of the present invention to provide a central processing unit (CPU) architecture with enhanced branch prediction, being substantially a pipelined CPU with multiple pipelines, each pipeline having a plurality of stages, by which all instructions relating directly to a branch instruction of a code executed by the pipelined CPU are being fetched respectively by each corresponding pipeline for enabling the code to be executed without stall so that the number of cycles required to execute the code can be reduced effectively. Moreover, the multiple pipelines can save more cycles when the number of stages in one pipeline is large.

Another object of the invention is to provide a CPU architecture with at least two pipelines, the CPU architecture having at least a stage shared by the plural pipelines, such that the CPU architecture can be cheaper and less complicated.

Other aspects and advantages of the present invention will become apparent from the following detailed description, taken in conjunction with the accompanying drawings, illustrating by way of example the principles of the present invention.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a function block diagram of a pipelined CPU with multiple pipelines according to a preferred embodiment of the present invention.

FIG. 2 is a chart depicting a specific code being executed by a pipelined CPU with single pipeline as the branch prediction hits.

FIG. 3 is a chart depicting a code being executed by a pipelined CPU with single pipeline as the branch prediction misses.

FIG. 4 is a chart depicting a code being executed by the pipelined CPU with two pipelines of FIG. 1 as the branch prediction hits.

FIG. 5 is a chart depicting a code being executed by the pipelined CPU with two pipelines of FIG. 1 as the branch prediction misses.

DESCRIPTION OF THE PREFERRED EMBODIMENT

For your esteemed members of reviewing committee to further understand and recognize the fulfilled functions and structural characteristics of the invention, several preferable embodiments cooperating with detailed description are presented as the follows.

Please refer to FIG. 1, which is a function block diagram of a pipelined CPU with multiple pipelines according to a preferred embodiment of the present invention. As seen in FIG. 1, the system of the pipelines CPU with multiple pipelines of FIG. 1 comprises a local memory 1, a memory controller 2 and a CPU 3. The CPU 3 further comprises a cache 31 and two pipelines, i.e. pipeline A and B, wherein pipeline A has four stages, i.e. fetch A stage 32, decode A stage 33, execute stage 36, and writeback stage 37, and pipeline B has four stages, i.e. fetch B stage 34, decode B stage 35, execute stage 36, and writeback stage 37, whereas the execute stage 36 and the writeback stage 37 are shared by the two pipelines. It is noted that there can be more pipelines, each having more stages, being designed in the pipelined CPU of the present invention.

Please refer to FIG. 2, which is a chart depicting a specific code being executed by a pipelined CPU with single pipeline as the branch prediction hits. Assuming the specific code being execute by the pipelined CPU with single pipeline is as following: CMP R1, R2 ; compare R1 value with R2 value JB process A ; if R1 is larger than R2, than jump to process A MOV R3, R2 ; else move R2 value to R3 process A; MOV R3, R1 ; move R1 value to R3

As seen in FIG. 2, there are four stages in the single pipeline. The first stage is the “FETCH” stage, which reads the instruction to be executed from memory. The second stage is the “DECODE” stage. This stage identifies the instruction to be executed and fetches any register-resident operands. The third stage is the “EXECUTE” stage. This stage performs an arithmetic or logical operation on the register-based operands. The fourth and final stage is the “WRITEBACK” stage. This stage writes the result of the execute stage into a register. Note that once the fetch stage is completed for the i-th instruction, the fetch stage may be started for the i+1st instruction. Likewise, the decode, execute and writeback stages be performed for the i+1st instruction immediately after completing work on the i-th instruction.

FIG. 2 shows what happens if a pipelined CPU with single pipeline is being used to execute the above code and the branch prediction is R1 larger than R2 while R1 is really larger than R2. In the Cycle 0, the instruction “CMP R1, R2” is fetched. In the Cycle 1 following the Cycle 0, the instruction “JB process A” is fetched while the instruction “CMP R1, R2” is decoded. In the Cycle 2 following the Cycle 1, the instruction “MOV R3, R1” is fetched since R1 is really larger than R2 enabling the process to jump to execute the process A, and the instruction “JB process A” is decoded while the instruction “CMP R1, R2” is being executed. In the Cycle 3 following the Cycle 2, the instruction “MOV R3, R1” is decoded, and the instruction “JB process A” is being executed while the result of the executed instruction “CMP R1, R2” is being written into a register. In the Cycle 4 following the Cycle 3, the instruction “MOV R3, R1” is being executed while the result of the executed instruction “JB process A” is being written into a register. In the Cycle 5 following the Cycle 4, the result of the executed instruction “MOV R3, R1” is being written into a register. Noted that the pipelined CPU with signal pipeline would take only 6 cycles, i.e. Cycle 0˜Cycle 5, to complete the above code when the branch prediction “hits”.

Please refer to FIG. 3, which is a chart depicting a specific code being executed by a pipelined CPU with single pipeline as the branch prediction misses. Assuming the specific being executed by the pipelined CPU with single pipeline is the same as that of FIG. 2 and the only difference is that the R1 is not larger than R2, i.e. the branch prediction “misses”.

Similarly, FIG. 3 shows what happens if a pipelined CPU with single pipeline is being used to execute the above code and the branch prediction is R1 larger than R2 while R1 is not larger than R2. In the Cycle 0, the instruction “CMP R1, R2” is fetched. In the Cycle 1 following the Cycle 0, the instruction “JB process A” is fetched while the instruction “CMP R1, R2” is decoded. In the Cycle 2 following the Cycle 1, the instruction “MOV R3, R1” is fetched, and the instruction “JB process A” is decoded while the instruction “CMP R1, R2” is being executed. In the Cycle 3 following the Cycle 2, the instruction “MOV R3, R2” is decoded, and the instruction “JB process A” is being executed while the result of the executed instruction “CMP R1, R2” is being written into a register. In the Cycle 4 following the Cycle 3, the instruction “MOV R3, R1” can not be executed since R1 is not larger than R2 and thus another instruction “MOV R3, R2” is fetched, while the result of the executed instruction “JB process A” is being written into a register. In the Cycle 5 following the Cycle 4, the instruction “MOV R3, R2” is decoded. In the Cycle 6 following the Cycle 5, the instruction “MOV R3, R2” is being executed. In the Cycle 7 following the Cycle 6, the result of the executed instruction “MOV R3, R2” is being written into a register. Noted that the pipelined CPU with signal pipeline would take 8 cycles, i.e. Cycle 0˜Cycle 7, to complete the above code when the branch prediction “misses”. The stall of performance is caused by the miss of branch prediction that direct the CPU to execute the “JB process A” and thus fetch the wrong instruction “MOV R3, R1” in the Cycle 2 instead of executing the correct instruction ““MOV R3, R2”. Since the CPU must discard the wrongfully fetched instruction “MOV R3, R1” and fetch again the correct instruction “MOV R3, R2”, two cycles are wasted.

Please refer to FIG. 4, which is a chart depicting a code being executed by the pipelined CPU with two pipelines of FIG. 1 as the branch prediction hits. The pipelined CPU with two pipelines of FIG. 1 are being used for executing the specific code the same as that of FIG. 2. The pipelined CPU has two pipelines, i.e. pipeline A and B, and the pipeline A has four stages, i.e. fetch A stage 32, decode A stage 33, execute stage 36, and writeback stage 37, and the pipeline B has four stages, i.e. fetch B stage 34, decode B stage 35, execute stage 36, and writeback stage 37, whereas the execute stage 36 and the writeback stage 37 are shared by the two pipelines.

FIG. 4 shows what happens if a pipelined CPU with two pipelines is being used to execute the above code and the branch prediction is R1 larger than R2 while R1 is really larger than R2. Assuming that the default pipeline of the CPU is the pipeline A. In the Cycle 0, the instruction “CMP R1, R2” is fetched by the default pipeline A. In the Cycle 1 following the Cycle 0, the instruction “JB process A” is fetched by the default pipeline A while the instruction “CMP R1, R2” is decoded. In the Cycle 2 following the Cycle 1, the instruction “MOV R3, R1” is fetched by the default pipeline A and the instruction “JB process A” is decoded while the instruction “CMP R1, R2” is being executed, and the same time the pipelined CPU detects the existence of the pipeline B. In the Cycle 3 following the Cycle 2, the instruction “MOV R3, R2” is fetched by the pipeline B, the instruction “MOV R3, R1” is decoded, and the instruction “JB process A” is being executed while the result of the executed instruction “CMP R1, R2” is being written into a register In the Cycle 4 following the Cycle 3, the instruction “MOV R3, R1” is being executed while the result of the executed instruction “JB process A” is being written into a register for enabling the pipelined CPU to detect that R1 is really larger than R2, i.e. the branch prediction hits, and then to discard the instructions in the pipeline B. In the Cycle 5 following the Cycle 4, the result of the executed instruction “MOV R3, R1” is being written into a register. Noted that the pipelined CPU with two pipelines would take only 6 cycles, i.e. Cycle 0˜Cycle 5, to complete the above code when the branch prediction “hits”, which is the same as that of the pipelined CPU with single pipeline.

Please refer to FIG. 5, which is a chart depicting a code being executed by the pipelined CPU with two pipelines of FIG. 1 as the branch prediction misses. Assuming the specific being executed by the pipelined CPU with two pipelines is the same as that of FIG. 4 and the only difference is that the R1 is not larger than R2, i.e. the branch prediction “misses”. Thus, in the Cycle 4, the CPU detect that R1 is not larger than R2 after the result of the executed instruction “JB process A” is being written into a register, i.e. the branch prediction misses, for enabling the CPU to discard the instructions in the pipeline A. And thereafter, the instruction in the pipeline B is being executed instead. Noted that the pipelined CPU with two pipelines would take only 7 cycles, i.e. Cycle 0˜Cycle 6, to complete the above code when the branch prediction “misses”, which is one cycle less than that of the pipelined CPU with single pipeline.

Moreover, by designing a pipelined CPU with multiple pipelines to have shared stages, the cost and the complexity of the pipelined CPU are reduced.

From the above description relating to FIG. 1 to FIG. 5, it is clear that the present invention provide a central processing unit (CPU) architecture with enhanced branch prediction, being substantially a pipelined CPU with multiple pipelines, each pipeline having a plurality of stages, by which all instructions relating directly to a branch instruction of a code executed by the pipelined CPU are being fetched respectively by each corresponding pipeline for enabling the code to be executed without stall so that the number of cycles required to execute the code can be reduced effectively. Moreover, the multiple pipelines can save more cycles when the number of stages in one pipeline is large.

While the preferred embodiment of the invention has been set forth for the purpose of disclosure, modifications of the disclosed embodiment of the invention as well as other embodiments thereof may occur to those skilled in the art. Accordingly, the appended claims are intended to cover all embodiments which do not depart from the spirit and scope of the invention. 

1. A central processing unit (CPU) architecture with enhanced branch prediction, being substantially a pipelined CPU having at least two pipelines, each pipeline having a plurality of stages, by which all instructions relating directly to a branch instruction of a code executed by the pipelined CPU are being fetched respectively by each corresponding pipeline for enabling the code to be executed without stall so as to reduce the number of cycles required to execute an instruction.
 2. The CPU architecture of claim 1, further comprising at least a stage shared by the at least two pipelines for reducing the cost and the complexity of the CPU architecture. 